//ztree
var setting = {data:{simpleData:{enable:true,idKey:"id",pIdKey:"parentId",rootPId:'0'}},
    callback:{onClick:function(event, treeId, treeNode){
        var code=treeNode.code;
        var  url;
        if(!code){
            return;
        }
        if(treeNode.isParent){
            url =base+"/employee/list?companyCode="+code;

        }
        if(!treeNode.isParent){
            url =base+"/employee/list?orgCode="+code;
        }
        if(!url){return;}
        $("#jqGrid").jqGrid('setGridParam',{url:url}).trigger("reloadGrid");
    }
    },
    async: {
        type:'post',
        enable: true,
        url:base+"/organization/view/treeData"
    }
};

function refreshTree(){
        $.fn.zTree.init($("#ztree"), setting, null)
}



function loadTreeTable(){
    $("#jqGrid").jqGrid($.extend(jqGrids.defaults,{
        url: base+'/employee/view/list',
        colNames:['ID','姓名', '邮箱','手机','状态','<div align="center" >操作</div>'],
        colModel: [
            {name:'id',index:'id',hidden:true, sortable:false},
            {name:'name',index:'name', width:'70',sortable:false},
            {name:'email',index:'email', width:'90',sortable:false},
            {name:'mobile',index:'mobile', width:'90',sortable:false},
            {name:'isEnabled',index:'isEnabled',width:'52',formatter:function(value,options,rData){
                return  value?'启用':'禁用';
            }},
            {name:'id',index:' ',width:'208',align:'center',sortable:false,formatter : function(value, options, rData){
                return "<a href='javascript:void(0)' onclick=doEdit(\""+value+"\")><span class='btn btn-sm purple'>编辑</span></a>";
                    // "<a href='javascript:void(0)' onclick=doAuthorization(\""+value+"\")><span class='btn btn-sm btn-danger'>授权</span></a>";
            }}
        ]

    }));

}


function doEdit(id) {
    var url = base+ "/employee/uc/edit?id=" + id + "&type=edit";
    window.location.href=url;
}

function doAdd(id) {
    var url =base+ "/employee/uc/edit?id=" + id + "&type=add";
    window.location.href=url;
}


function doSubmit(type) {
    if (!formCheck().form()) {
        return;
    }
    var url;

    if ("add" == type) {
        url = base + "/employee/sk/insert";
    } else {
        url = base + "/employee/uc/update";
    }
    $.post(url, $("#editForm").serialize(), function (data) {
        $.messager.alert0({selector: "#editAlert", type: data.code, html: data.message});
        if (data && data.code == 'SUCCESS') {
            tools.goto(base + "/employee/view/index", 500);
        }
    }, "json");

}


function formCheck() {
    return $('#editForm').validate({
        rules: {
            name: {
                maxlength:[24],
                required:true
            },
            no: {
                wRangeLength:[4,20],
                required:true
            },
            email: {
                email:true
            },
            mobile: {
                mobile:true
            },
            phone: {
                phone:true
            },
            companyName: {
                required:true
            },
            organizationName: {
                required:true
            },
            description: {
                maxlength:[100]
            }
        }
    });
}

/**
 *删除
 */
function doDelete() {
   var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
    if(ids.length<=0){
        $.messager.alert("请选择需要删除的数据项");
        return;
    }
    var url =base+"/employee/dh/delete";
    $.messager.confirm("你确定要删除选中的记录吗?", function (ck) {
        if(ck){
            $.post(url, {ids: ids}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                    $("#jqGrid").trigger("reloadGrid");
                }
            });
        }})
}


/**
 * 授权界面
 */
function  doAuthorization(id){
    var url = base+ "/employee/view/authorization/edit?id=" + id ;
    window.location.href=url;

}


function doInit(id){
    var url = base+ "/employee/password/init";
    $.messager.confirm("你确定要初始化密吗?", function (ck) {
        if (ck) {
            $.post(url, {id: id}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
            });
        }
    });
}


function changeLocked(){
	 var ids=$('#jqGrid').jqGrid('getGridParam','selarrrow');
	 if(ids.length<=0){
		  $.messager.alert("请选择需要解锁的用户");
        return;
	 }
	 var  url =base+"/employee/changeLocked?ids="+ids;  

	 $.messager.confirm("确定解锁选中用户吗?", function (ck) {
	 if(ck){
		 $.post(url, {ids: ids}, function (data) {
                $.messager.alert0({selector: "#indexAlert", type: data.code, html: data.message});
                if (data && data.code == 'SUCCESS') {
                    $("#jqGrid").trigger("reloadGrid");
                }
            });
	 }})
}
